//
//  LeftDetailViewController.m
//  BeautyClother
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "LeftDetailViewController.h"
#import "AsyncConnection.h"
#import "UIImageView+WebCache.h"
#import "ImageWithLabel.h"
#import "LeftDetailTableViewCell.h"
#import "womenDetailViewController.h"
#import "UMSocial.h"


@interface LeftDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate,UMSocialUIDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;
@property (nonatomic, retain) UITableViewCell *celltitle;
@property (nonatomic, retain) UITableViewCell *cellfrom;
@property (nonatomic, retain) UITableViewCell *celldesc;
@property (nonatomic, retain) UITableViewCell *cell;
@property (nonatomic, copy) NSString *iid;

@end

@implementation LeftDetailViewController

- (void)dealloc
{
    [_table release];
    [_array release];
    [_celltitle release];
    [_cellfrom release];
    [_celldesc release];
    [_cell release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.image = [[UIImage alloc]init];
    [self connection];
    [self createTableView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
    self.offHeight = [[UIScreen mainScreen] bounds].size.height / 667;
    self.title = @"Items";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
    //col=0&part=2&temp=20&occ=0&token=1416b90f03d637a168f695105aa4132d7&page=1&rule=201&ua=iPhone%7C1.7.5&lang=en&sig=7db6285676d61441693477565&nt=wifi
    
}

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)connection
{
    NSDictionary *dic = @{@"col":@"0",
                          @"part":@"2",
                          @"temp":@"20",
                          @"occ":@"0",
                          @"token":@"1416b90f03d637a168f695105aa4132d7",
                          @"page":@"1",
                          @"rule":self.lid,
                          @"ua":@"iPhone%7C1.7.5",
                          @"ang":@"en",
                          @"sig":@"7db6285676d61441693477565",
                          @"nt":@"wifi"};
    [AsyncConnection asyncConnection:@"http://api.palshock.cn/ic/v2/get_itemlist" parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [dic objectForKey:@"items"];
        [self.table reloadData];
    } path:[@"_comm_left" stringByAppendingString:self.lid]];
}

- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 69, self.view.frame.size.width, self.view.frame.size.height - 112) style:UITableViewStylePlain];
    
    [self.view addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.table release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count == 0) {
        return 0;
        
    }else
    {
        return 6;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
            self.cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
            if (_cell == nil) {
                _cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
            }
            
            UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(100 * _offWidth, 0, self.view.frame.size.width - 100 * _offWidth, 200 * _offHeight)];
            [_cell.contentView addSubview:image2];
            image2.tag = 101;
            image2.layer.borderWidth = 0.2;
            image2.layer.borderColor = [[UIColor colorWithRed:255/255 green:0 blue:255/255 alpha:0.5]CGColor];
            [image2 release];
            
            
            UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100 *_offWidth, 100 * _offHeight)];
            image1.tag = 100;
            [_cell.contentView addSubview:image1];
            [image1 release];
            
            
            UIImageView *image3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100 * _offHeight, 100 *_offWidth, 100 * _offHeight)];
            image3.tag = 102;
            [_cell.contentView addSubview:image3];
            [image3 release];
            
            UIImageView *image4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200 * _offHeight, self.view.frame.size.width / 3, 100 * _offHeight)];
            image4.tag = 103;
            [_cell.contentView addSubview:image4];
            [image4 release];
            
            UIImageView *image5 = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3, 200 * _offHeight, self.view.frame.size.width / 3, 100 * _offHeight)];
            image5.tag = 104;
            [_cell.contentView addSubview:image5];
            [image5 release];
            
            UIImageView *image6 = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 * 2, 200 * _offHeight, self.view.frame.size.width / 3, 100 * _offHeight)];
            image6.tag = 105;
            [_cell.contentView addSubview:image6];
            [image6 release];
            
            for (int i = 0; i < self.arr.count; i++) {
                UIImageView *first = (UIImageView *)[_cell.contentView viewWithTag:100 + i];
                [first sd_setImageWithURL:[NSURL URLWithString:[[self.arr objectAtIndex:i]objectForKey:@"url"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
            }


//        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
//        [cell.contentView addSubview:image];
//            [image sd_setImageWithURL:[NSURL URLWithString:[[self.arr objectAtIndex:1]objectForKey:@"url"]] placeholderImage:nil];
            return _cell;
    }
    if (indexPath.row == 1) {
        LeftDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        if (cell == nil) {
            cell = [[LeftDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        }
    cell.liuxing.delegate = self;
    cell.liuxing.dataSource  = self;
    [cell.liuxing registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        return cell;
    }if (indexPath.row == 2) {
    
        self.celltitle = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
        if (self.celltitle == nil) {
            self.celltitle = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
            self.celltitle.textLabel.text = [@"$" stringByAppendingString:[[[self.arr objectAtIndex:1]objectForKey:@"prc"]stringValue]];
            
        }
        return self.celltitle;
    }
    if (indexPath.row == 3) {
        
        self.cellfrom = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
        if (self.cellfrom == nil) {
            self.cellfrom = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
            
            self.cellfrom.textLabel.text = [[self.arr objectAtIndex:1]objectForKey:@"title"];
            self.cellfrom.textLabel.font = [UIFont systemFontOfSize:15];
            self.cellfrom.textLabel.textColor = [ UIColor darkGrayColor];
            
        }
        return self.cellfrom;
    }
    if (indexPath.row == 4) {
        
        self.celldesc = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
        if (self.self.celldesc == nil) {
            self.self.celldesc = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
//            self.celldesc.textLabel.text = [@"From:   " stringByAppendingString:[[self.arr objectAtIndex:1]objectForKey:@"from"]];
        }
        return self.celldesc;
    }


    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [NSString stringWithFormat:@"%ld", indexPath.row]];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
        }
        
            
            NSArray *a = @[@"che.png", @"fen.png",@"BUY", @"SHARE"];
            for (int i = 0; i < 2; i ++) {
                ImageWithLabel *image = [[ImageWithLabel alloc]initWithFrame:CGRectMake(130 *_offWidth * i + 70 * _offWidth, 0, 150 * _offWidth, 100 *_offHeight)];
                [cell.contentView addSubview:image];
                
                image.ima.image = [UIImage imageNamed:[a objectAtIndex:i]];
                image.title.text = [a objectAtIndex:i + 2];
                if ([image.title.text isEqual:@"BUY"]) {
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buy:)];
                    image.userInteractionEnabled = YES;
                    [image addGestureRecognizer:tap];
                 
                    
                }
                
                if ([image.title.text isEqual:@"SHARE"]) {
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(share:)];
                    image.userInteractionEnabled = YES;
                    [image addGestureRecognizer:tap];
                    
                }
                [image release];
                
            }
        return cell;
        }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 300 * _offHeight;
    }
    if (indexPath.row == 1) {
        return 100 * _offHeight;
    }
    if (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4) {
        return 50 * _offHeight;
    }else
    {
        return 100 * _offHeight;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
    [cell.contentView addSubview:image];
    [image sd_setImageWithURL:[NSURL URLWithString:[[self.array objectAtIndex:indexPath.item]objectForKey:@"url"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [((UIImageView *)[self.cell.contentView viewWithTag:101]) sd_setImageWithURL:[[self.array objectAtIndex:indexPath.item]objectForKey:@"url"] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    self.celltitle.textLabel.text = [@"￥" stringByAppendingString:[[[self.array objectAtIndex:indexPath.item]objectForKey:@"prc"]stringValue]];
    self.cellfrom.textLabel.text = [[self.array objectAtIndex:indexPath.item]objectForKey:@"title"];
    self.celldesc.textLabel.text = [@"From:   " stringByAppendingString:[[self.array objectAtIndex:indexPath.item]objectForKey:@"from"]];
    self.iid = [[self.array objectAtIndex:indexPath.item]objectForKey:@"id"];
    
    
}

- (void)buy:(UIButton *)sender
{
    //http://www.farfetch.com/LinkShareUS.aspx?siteID=nipSqzzv2p0-EaYiHkuZfzWP9gSwybq6iw&url=http%3A%2F%2Fwww.farfetch.com%2Fshopping%2Fwomen%2Fitem11107656.aspx
    womenDetailViewController *wareWeb = [[womenDetailViewController alloc]init];
    NSString *str = [NSString stringWithFormat:@"%@%@%@", @"http://www.farfetch.com/LinkShareUS.aspx?siteID=nipSqzzv2p0-EaYiHkuZfzWP9gSwybq6iw&url=http%3A%2F%2Fwww.farfetch.com%2Fshopping%2Fwomen%2Fitem", self.iid, @".aspx"];
    NSURL *url = [NSURL URLWithString:str];
    wareWeb.url = url;
    [self.navigationController pushViewController:wareWeb animated:YES];
    [wareWeb release];
}

- (void)share:(UIButton *)button
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55e57550e0f55a57db000e09"
                                      shareText:_celltitle.textLabel.text
                                     shareImage:((UIImageView *)[self.cell.contentView viewWithTag:101]).image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
